package com.consume.bhy.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

public class JDBC_Util {

	private static final ResourceBundle rb =ResourceBundle.getBundle("p");
	private static final String URL =  rb.getString("URL");
	private static final String USER  = rb.getString("USER");;
	private static final String PASSWORD = rb.getString("PASSWORD");
	private static final String DRIVER = rb.getString("DRIVER");

	private JDBC_Util() { throw new RuntimeException("不能实例化当前类！");}

	static{
			try {
				Class.forName(DRIVER);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}			
	}
		
	public static Connection getConnection() {
		Connection connection = null;

		try {
			connection = DriverManager.getConnection(URL, USER, PASSWORD);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
		
	
	public static PreparedStatement getPreparedStatement(String sql) {

		PreparedStatement prepared = null;
		Connection conn = getConnection();
		try {
			prepared = conn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			clossJDBC(conn);
		}
		return prepared;
	}
		
	public static boolean clossJDBC(Connection conn) {
		
		try {
			if(conn != null) {
				conn.close();
			}
			
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean clossJDBC(PreparedStatement prepared) {
		
		try {
			if(prepared != null) {
				prepared.close();
			}
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean clossJDBC(PreparedStatement prepared,ResultSet result) {
		
		try {
			if(result != null) {
				result.close();
			}
			return clossJDBC(prepared);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean clossJDBC(Connection conn,PreparedStatement prepared) {
		
		try {
			if(prepared != null) {
				prepared.close();
			}
			return clossJDBC(conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}	
	
	public static boolean clossJDBC(Connection conn,PreparedStatement prepared,ResultSet result) {
		
		
		try {
			if(result != null) {
				result.close();
			}
			return clossJDBC(conn,prepared);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean updateSql(Map<String,List<Object>> map){
		
		Connection connection = getConnection();
		PreparedStatement preparedStatement = null;
		List<Object> valueList = new ArrayList<Object>();	
		try {
			connection.setAutoCommit(false);
		} catch (SQLException e2) {
			e2.printStackTrace();
		}
		
		try {
			for(String key : map.keySet()) {
			
				preparedStatement = connection.prepareStatement(key);
				
				valueList = map.get(key);
				for (int j = 0; j < valueList.size(); j++) {
					
				preparedStatement.setObject(j+1, valueList.get(j));
					
				}
				
				preparedStatement.executeUpdate();
				preparedStatement.close();
				
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
			return false;
		}
		try {
			connection.commit();
			clossJDBC(connection);
			return true;
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return false;

	}

	
}
